//
//  GLPresentationController.swift
//  XLWB
//
//  Created by 郭亮 on 16/3/15.
//  Copyright © 2016年 gl. All rights reserved.
//

import UIKit

class GLPresentationController: UIPresentationController {
    // MARK:-属性
    var presentedFrame: CGRect = CGRectZero
    
    // MARK:-懒加载属性
    private lazy var coverView: UIView = UIView()
    
    // MARK:-系统回调函数
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        //设置弹出的view的位置跟属性
        presentedView()?.frame = presentedFrame
        //添加蒙板
        addCoverView()
    }
}

extension GLPresentationController {
    private func addCoverView() {
        coverView.frame = containerView!.bounds
        coverView.backgroundColor = UIColor(white: 0.8, alpha: 0.2)
        let tap = UITapGestureRecognizer(target: self, action: "coverViewClick")
        coverView.addGestureRecognizer(tap)
        //添加蒙板
        containerView?.insertSubview(coverView, belowSubview: presentedView()!)
    }
    
    @objc private func coverViewClick() {
        //销毁弹出控制器
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}
